/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

/**
a segmented progress bar to display playlists 
*/
self.ProgressBar = function(container,playlist)
{
	this.el_main = Element("div",{className:"segmented_progress_bar ui-corner-all"},container);
	this.total_duration = 0;
	this.segments = Array();
		
	//calculate duration of playlist
	for(var i=0;i<playlist.length;i++)
	{
		this.total_duration += parseInt(playlist[i].duration);
	}
	
	//add segments for all playlist items
	pixel_width = $(this.el_main).width()
	r_width = 0 
	for(var i=0;i<playlist.length;i++)
	{	
		width = (pixel_width*(playlist[i].duration / this.total_duration))
		r_width += width
		if(r_width > pixel_width) //due to rounding errors we may use more pixels 
		{
			width -= 1
		}
		segment = new ProgressBarSegment(this.el_main,playlist[i],width,i)
		this.segments.push(segment)
	}
	
	//there should always be one
	this.current_segment = this.segments[0];
	
	//we listen to ...
	$(document).bind("annotation_cancel",bind(this,"clearRecording")); //annotation maker cancel
	$(document).bind("annotation_done",bind(this,"clearRecording")); //annotation maker done
	$(document).bind("annotation_click",bind(this,"showRecording")); //annotation browser click
	$(document).bind("annotation_edit_preview",bind(this,"showRecording")); //annotation maker time edit
	$(document).bind("player_progress",bind(this,"onPlayerProgress")); //playlist player progressing

	//make playhead
	this.el_play_head = Element("div",{className:"pb_play_head"},container);

}
/**
tell the current remove the highlighting
*/
ProgressBar.prototype.clearRecording = function()
{
	this.current_segment.clearHighlight()
}
/**
tell the current remove the highlighting, find the segment with the annotation??
*/
ProgressBar.prototype.showRecording = function(evt,channel,annotation)
{
	s = this.segments.findObj("playlist_item",annotation)
	if(s == null)
	{
		this.current_segment.setHighlight(annotation)
	}
	else
	{
		s.setHighlight(annotation)
	}
}
/**
called from external event, bar should redraw
*/
ProgressBar.prototype.onPlayerProgress = function(evt,playlist_item,time_in_movie,time_in_fragment)
{	
	
	segment = this.segments.findObj("playlist_item",playlist_item); //find the current segment by its playlist_item
	if(segment != this.current_segment) //a new segmented has started
	{
		this.current_segment = segment;
		earlier_part = true;
		for(var i=0;i<this.segments.length;i++)
		{
			s = this.segments[i];
			s.clearHighlight()
			if(	s == this.current_segment )
			{
				s.redraw(time_in_fragment,this.el_play_head)
				earlier_part = false;	
			}
			else if(earlier_part)
			{
				s.redraw(s.playlist_item.duration,null)
			}
			else
			{
				s.redraw(0,null)
			}
		}
	}
	else
	{
		segment.redraw(time_in_fragment,this.el_play_head)
	}
	
}
/**
create a segment for the progress bar
*/
function ProgressBarSegment(container,playlist_item,width,index)
{
	el_segment = Element("div",{className:"pb_segment"},container);
	el_segment.style.width = width +"px";
	el_segment.onclick = bind(this,"onClick",[])
	el_segment.el_progress = Element("div",{className:"pb_segment_progress"},el_segment);
	if(index > 0)
	{
		el_segment.el_progress.className = el_segment.el_progress.className + " not_first" 
	}
	el_segment.el_progress.el_highlight = Element("div",{className:"pb_progress_highlight"},el_segment.el_progress);

	
	this.el_segment = el_segment;
	this.playlist_item = playlist_item;
	this.current_time = 0;
	this.pixel_width = width;
}
/**
segment was clicked, find out at which position in the segment
*/
ProgressBarSegment.prototype.onClick = function(evt)
{
	offset = $(this.el_segment).offset();
	item_pixel_offset = evt.clientX - offset.left;
	item_secs_offset = (item_pixel_offset * this.playlist_item.duration ) / $(this.el_segment).width()
	$(document).trigger(jQuery.Event("progress_bar_click"),[this.playlist_item,item_secs_offset]);
}
/**
segment was clicked, find out at which position in the segment
*/
ProgressBarSegment.prototype.redraw = function(time_played,pointer)
{
	this.current_time = time_played
	w = (this.pixel_width * time_played) / this.playlist_item.duration
	this.el_segment.el_progress.style.width = w + "px"; //update progress bar
	
	
	if(pointer)
	{
		this.el_segment.el_progress.appendChild(pointer)
		$(pointer).text(AnnotraxTime.secondsToTimeString(time_played))
		pointer.style.left = w + "px";
	}
	
	
/*	if(this.update_highlighting) //adapt begin and end time if highlighting is on
	{
		if(this.highlight_begin == null)
		{
			this.highlight_begin = time_played
		}
		this.highlight_end = time_played
	} */
	//playhead
/*	if(this.el_current_segment.playhead == null) //this segment became the playhead segment
	{
		this.el_current_segment.playhead = this.el_play_head;
		this.el_current_segment.appendChild(this.el_play_head)
	}
	this.el_play_head.style.left = played_perc + "%"
	offset = $(this.el_play_head).offset();*/

	//progress
	
	
	
	//recording progress
/*	if(this.el_current_segment.recording == true) //this segment became the recording segment
	{
		if(this.el_current_segment.rec_progress == null)
		{
			this.el_current_segment.rec_progress = this.el_rec_progress;
			this.el_current_segment.progress.appendChild(this.el_rec_progress);	
			this.el_rec_progress.rec_started = time_played;	
			
			this.el_rec_progress.style.left = offset.left + "px";
			this.el_rec_progress.left = offset.left
			//this.el_rec_progress.style.left = "50px" ;//played_perc + "%";
			
				
		}
	
		rec_perc = ( (time_played - this.el_rec_progress.rec_started) / this.el_current_segment.playlist_item.duration) * 100
		this.el_rec_progress.style.width = (offset.left - this.el_rec_progress.left) + "px"
		
	}*/
}
/**

*/
ProgressBarSegment.prototype.clearHighlight = function()
{
	this.highlight_begin = null;
	this.highlight_end = null;
	$(this.el_segment.el_progress.el_highlight).hide("fade",1000)
}
/**
can be a real annotation or one that is being made
*/
ProgressBarSegment.prototype.setHighlight = function(annotation)
{
	end = annotation.time + annotation.duration;
	//console.log(annotation)
	left = (this.pixel_width * annotation.time) / this.playlist_item.duration
	this.el_segment.el_progress.el_highlight.style.left = left + "px";
	right = (this.pixel_width * end) / this.playlist_item.duration
	//console.log(right)
	width = (right - left)
	if(width < 1)
	{
		width = 1
	}		
	this.el_segment.el_progress.el_highlight.style.width = width + "px";
	$(this.el_segment.el_progress.el_highlight).show()
}


/** 

*//*
ProgressBar.prototype.updateCuePoints = function(segment,time_played)
{
	//add playhead to current segment if needed
	if(segment.lastChild != this.playHead)
	{
		segment.appendChild(this.playHead)
	}
	//progress
	percentage = (time_played / segment.playlist_item.duration) * 100
	segment.progress.style.width = "" + percentage + "%"; //update progress bar
	
}*/


